package com.springmybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springmybatisplus.entity.Book;
import com.springmybatisplus.mapper.BookMapper;
import com.springmybatisplus.service.BookService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2023-11-29
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper,Book> implements BookService {
	@Resource
	private BookMapper bookMapper;
	@Override
	public List<Book> findAllBooks() {
		return bookMapper.selectList(null);
	}

	@Override
	public Book findBookById(Integer id) {
		return bookMapper.selectById(id);
	}

	@Override
	public boolean addBook(Book book) {
		boolean flag=false;
		if (bookMapper.insert(book)>0){
			flag=true;
		}
		return flag;
	}

	@Override
	public boolean updateBook(Book book) {
		boolean flag=false;
		if (bookMapper.updateById(book)>0){
			flag=true;
		}
		return flag;
	}

	@Override
	public boolean deleteById(Integer id) {
		boolean flag=false;
		if (bookMapper.deleteById(id)>0){
			flag=true;
		}
		return flag;
	}

	@Override
	public List<Book> searchBooks(Book book) {
		/*QueryWrapper<Book> queryWrapper=new QueryWrapper<>();
		queryWrapper.like(book.getName()!=""&&book.getName()!=null,"name",book.getName());
		queryWrapper.eq(book.getCategory()!=""&&book.getCategory()!=null,"category",book.getCategory());
		queryWrapper.eq(book.getAuthor()!=""&&book.getAuthor()!=null,"author",book.getAuthor());*/
		LambdaQueryWrapper<Book> queryWrapper=new LambdaQueryWrapper<>();
		queryWrapper.like(book.getName()!=""&&book.getName()!=null,Book::getName,book.getName());
		queryWrapper.eq(book.getCategory()!=""&&book.getCategory()!=null,Book::getCategory,book.getCategory());
		queryWrapper.eq(book.getAuthor()!=""&&book.getAuthor()!=null,Book::getAuthor,book.getAuthor());
		return bookMapper.selectList(queryWrapper);
	}

	@Override
	public IPage<Book> getPage(Integer start, Integer size) {
		IPage<Book> page=new Page<>(start,size);
		return bookMapper.selectPage(page,null);
	}
}
